package com.tanhua.manage.utils;

import cn.hutool.core.date.DateUtil;

import java.math.BigDecimal;
import java.util.Date;

public class ComputeUtil {
    /**
     * 计算环比
     *
     * @param current 本期计数
     * @param last    上一期计数
     * @return 环比
     */
    public static BigDecimal computeRate(Integer current, Integer last) {
        BigDecimal result;
        if (last == 0) {
            // 当上一期计数为零时，此时环比增长为倍数增长
            result = new BigDecimal((current - last) * 100);
        } else {
            result = BigDecimal.valueOf((current - last) * 100).divide(BigDecimal.valueOf(last), 2, BigDecimal.ROUND_HALF_DOWN);
        }
        return result;
    }

    public static String offsetDay(Date date,int offset) {
        return DateUtil.offsetDay(date,offset).toDateStr();
    }

    public static String offsetYear(String date) {
        int year = Integer.parseInt(date.split("-")[0]);
        //判断是闰年还是平年
        if(DateUtil.isLeapYear(year)){
            //如果是闰年-366返回去年的时间
            Date dates=DateUtil.parse(date,"yyyy-MM-dd");
            return DateUtil.offsetDay(dates,-366).toDateStr();
        }else {
            //否则-365
            Date dates=DateUtil.parse(date,"yyyy-MM-dd");
            return DateUtil.offsetDay(dates,-365).toDateStr();
        }
    }

   /* public static void main(String[] args) {
        String year = offsetYear("2010-06-20");
        String year1 = offsetYear("2010-06-10");
        System.out.println("year1 = " + year1);
        System.out.println("year = " + year);
    }*/
}